package com.csun.cmny.provider.controller;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.domain.OmcOldmanExcrementRecord;
import com.csun.cmny.provider.model.dto.excrement.ExcrementRecordSaveDto;
import com.csun.cmny.provider.model.dto.excrement.ExcrementRecordSelectDto;
import com.csun.cmny.provider.model.vo.excrement.ExcrementRecordVo;
import com.csun.cmny.provider.pojo.Oldman;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OldmanService;
import com.csun.cmny.provider.service.OmcOldmanExcrementRecordService;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/excrement", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcOldmanExcrementRecordController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private OmcOldmanExcrementRecordService excrementRecordService;

    @Resource
    private OldmanService oldmanService;

    /*新增老人大便记录*/
    @ValidateAnnotation
    @RequestMapping(value = "/addExcrementRecord", method = RequestMethod.POST)
    public Wrapper addSanitationService (@RequestBody @Valid ExcrementRecordSaveDto excrementRecordSaveDto, BindingResult br){


        OmcOldmanExcrementRecord omcOldmanExcrementRecord = new OmcOldmanExcrementRecord();

        BeanUtils.copyProperties(excrementRecordSaveDto,omcOldmanExcrementRecord);

        omcOldmanExcrementRecord.setRecordDate(new Date());
        omcOldmanExcrementRecord.setGroupId(getLoginAuthDto().getGroupId());


        int result = excrementRecordService.save(omcOldmanExcrementRecord);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /*查看老人大便记录*/
    @ValidateAnnotation
    @RequestMapping(value = "/selectExcrementRecord",method = RequestMethod.POST)
    public Wrapper selectExcrementRecord(@RequestBody @Valid ExcrementRecordSelectDto excrementRecordSelectDto,BindingResult br){

        List<ExcrementRecordVo> list=excrementRecordService.selectExcrementRecord(excrementRecordSelectDto);

        PageInfo<ExcrementRecordVo> pageInfo = new PageInfo<>(list);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success",null),pageInfo);
    }
}
